<?php

class Twitter {

	 private $name;
	 private $modPassword;
	 private $rssURL = null;
	 private $reqToken = null;
	 private $oAuthToken = null;
	 private $user_id = null;
	 private $user_name = null;
	 private $oAuthTokenSecret = null;
	 private $channels = array();
	 private $channelsPtr = 0;
	 private $entries = array();

	 public function __construct($name, $modPassword, $reqToken) {
		  $this->name = $name;
		  $this->modPassword = $modPassword;
		  $this->reqToken = $reqToken;
	 }

	 public function __get($name) {
		  if (isset($this->$name) && !is_array($this->$name)) {
				return $this->$name;
		  }

		  return false;
	 }

	 public function setTokens($oAuthToken, $oAuthTokenSecret) {
		  $this->oAuthToken = $oAuthToken;
		  $this->oAuthTokenSecret = $oAuthTokenSecret;
	 }

	 public function setDetails($id, $name) {
		  $this->user_id = $id;
		  $this->user_name = $name;
	 }

	 public function poll() {
		  if ($this->oAuthToken != null && $this->oAuthTokenSecret != null) {
				$twitterLib = new TwitterLib(CONSUMER_KEY, CONSUMER_SECRET);
				$twitterLib->setOAuthToken($this->oAuthToken);
				$twitterLib->setOAuthTokenSecret($this->oAuthTokenSecret);

				$tweets = $twitterLib->statusesUserTimeline();

				$rtn = array();

				foreach ($tweets as $tweet) {
					 if (!$this->entryExists($tweet['id_str'])) {
						  $rtn[] = $tweet['text'];
						  $this->addEntry($tweet['id_str']);
					 }
				}

				return $rtn;
		  }
	 }

	 public function addChan($chan) {
		  $this->channels[] = $chan;
	 }

	 public function delChan($chan) {
		  $tmp = array();

		  foreach ($this->channels as $channel) {
				if ($channel != $chan) {
					 $tmp[] = $channel;
				}
		  }

		  $this->channels = $tmp;
	 }

	 public function numChans() {
		  return count($this->channels);
	 }

	 public function getChansCSV() {
		  return implode(',', $this->channels);
	 }

	 public function addEntry($entry) {
		  $this->entries[] = (string) $entry;
	 }

	 public function entryExists($entry) {
		  return in_array($entry, $this->entries);
	 }

	 public function save($file) {
		  file_put_contents($file, serialize($this));
	 }

	 public function hasNextChannel() {
		  if ($this->channelsPtr < count($this->channels)) {
				return true;
		  }

		  return false;
	 }

	 public function nextChannel() {
		  return $this->channels[$this->channelsPtr++];
	 }

	 public function resetChannelIterator() {
		  $this->channelsPtr = 0;
	 }

	 public function checkPass($pass) {
		  if ($this->modPassword == $pass) {
				return true;
		  } else {
				echo "~{$this->modPassword}~ != ~{$pass}~";
		  }

		  return false;
	 }

	 public function isChan($chan) {
		  foreach ($this->channels as $channel) {
				if ($channel == $chan) {
					 return true;
				}
		  }

		  return false;
	 }

}

?>
